<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.processCode" clearable value-key-name="code" placeholder="工序" :option-list="processList" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<shift-select v-model="searchParams.shiftId" clearable placeholder="班次" :option-list="shiftList" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></shift-select>
					<day-switch v-model="searchParams.belongDate" size="small" class="item-gap"></day-switch>
					<div class="search-bar-item-size2 item-gap">
						<el-date-picker v-model="searchParams.belongDate" :clearable="false" size="small" style="width: 100%;" type="date" value-format="yyyy-MM-dd" placeholder="日期" />
					</div>
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table
				ref="jkTable"
				border
				:footer-method="footerMethod"
				show-footer
				:loading="tableLoading"
				:data="tableData"
				:height="tableHeight + 20"
			>
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<span>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatDate } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import { productionPrdReportGetProcessDayData } from '../../../api/production/reports';
    import XEUtils from 'xe-utils';
    import { workforcemanagementShiftPage } from '../../../api/schedule/shift';
    import daySwitch from '../../jk-components/jk-day-switch';
    import shiftSelect from '../../jk-components/jk-shift-select';
    export default {
        name: 'ProcessYieldDailyReport',
        components: {
            shiftSelect,
            daySwitch
        },
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                apiItem: {
                    'page': productionPrdReportGetProcessDayData
                },
                shiftList: [],
                processList: [],
                workshopList: [],
                tableLoading: true,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    belongDate: '',
                    processCode: null
                },
                noCalKey: ['machineName', 'productName', 'batchCode']

            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            footerMethod({ columns, data }) {
                return [
                    columns.map((column, columnIndex) => {
                        if (columnIndex === 0) {
                            return `合计`;
                        }
                        if (!this.noCalKey.includes(column.property)) {
                            return XEUtils.sum(data, column.property);
                        }
                        return null;
                    })
                ];
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            getShiftListRequest() {
                return workforcemanagementShiftPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.shiftList = res.data.res || [];
                        this.$set(this.searchParams, 'shiftId', this.shiftList && this.shiftList.length ? this.shiftList[0].id : null);
                    }
                });
            },
            async getDependentData() {
                this.processList = this.$store.state.user.userProcess.processList;
                this.$set(this.searchParams, 'processCode', this.$route.query.processCode || this.$store.state.user.userProcess.defaultProcessCode);
                this.$set(this.searchParams, 'belongDate', this.$route.query.belongDate || formatDate(Date.now()));
                // this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;

                this.tableLoading = true;
                await this.getShiftListRequest();
                await this.getMenuColumnListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = { ...this.searchParams };
                return this.apiItem['page'](params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.totalCount = 1;
                this.getListRequest();
            }
        }
    };
</script>
<style>
</style>
